package com.xiaouyudeguang.common.collect;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListBuilder<E> {

    private final List<E> list = new ArrayList<>();

    private ListBuilder(List<E> list) {
        addAll(list);
    }

    public ListBuilder<E> add(E e) {
        this.list.add(e);
        return this;
    }

    public ListBuilder<E> addAll(List<E> list) {
        if (list != null && !list.isEmpty()) {
            this.list.addAll(list);
        }
        return this;
    }

    public List<E> build() {
        return list;
    }

    @SafeVarargs
    public static <E> ListBuilder<E> builder(E... es) {
        return new ListBuilder<>(Arrays.asList(es));
    }

    public static <E> ListBuilder<E> builder(List<E> list) {
        return new ListBuilder<>(list);
    }
}
